﻿using Microsoft.EntityFrameworkCore;
using Simple.Common.EventBus;
using Simple.Services.System.MorningCheck;
using Simple.Services.System.MorningCheck.Models;

namespace Simple.Services;

public class DeviceEventModel : EventModel
{
    public BatchSyncDataQueryModel syncDataQueryModel { get; set; }

    public string ResponseStr { get; set; }

    public BatchSyncChildrensMorningCheckQueryModel batchSyncChildrensMorningCheckQueryModel { get; set; }

    public bool IsSyncData { get; set; }

    /// <summary>
    /// 同步类型：1:考勤 2:晨检
    /// </summary>
    public int SyncType { get; set; }
}

public class DeviceEventHandler : IEventHandler<DeviceEventModel>
{
    private readonly ClockingInMorningCheckService _clockingInMorningCheckService;

    public DeviceEventHandler(ClockingInMorningCheckService clockingInMorningCheckService)
    {
        _clockingInMorningCheckService = clockingInMorningCheckService;
    }
    public async Task Handle(DeviceEventModel @event)
    {
        if (@event.syncDataQueryModel != null)
        {
            await _clockingInMorningCheckService.SyncDataAsync(@event.syncDataQueryModel);
        }
        else if (!string.IsNullOrEmpty(@event.ResponseStr))
        {
            if (@event.IsSyncData)
            {
                await _clockingInMorningCheckService.SyncCallBack(@event.ResponseStr, @event.SyncType);

            }
            else
            {
                await _clockingInMorningCheckService.AttendanceCallback(@event.ResponseStr);
            }


        }
        else if (@event.batchSyncChildrensMorningCheckQueryModel != null)
        {
            await _clockingInMorningCheckService.SyncChildrensMorningCheck(@event.batchSyncChildrensMorningCheckQueryModel);
        }

    }
}
